﻿using System;
using System.Collections.Generic;
using System.IO;
using Terraria.Localization;
using Terraria.Net.Sockets;

namespace Terraria.Net
{
	// Token: 0x0200014C RID: 332
	public class NetManager
	{
		// Token: 0x06000E7D RID: 3709 RVA: 0x0000DFAB File Offset: 0x0000C1AB
		private NetManager()
		{
		}

		// Token: 0x06000E7E RID: 3710 RVA: 0x003FFD28 File Offset: 0x003FDF28
		public void Register<T>() where T : NetModule, new()
		{
			T t = Activator.CreateInstance<T>();
			NetManager.PacketTypeStorage<T>.Id = this._moduleCount;
			NetManager.PacketTypeStorage<T>.Module = t;
			this._modules[this._moduleCount] = t;
			this._moduleCount += 1;
		}

		// Token: 0x06000E7F RID: 3711 RVA: 0x0000DFBE File Offset: 0x0000C1BE
		public NetModule GetModule<T>() where T : NetModule
		{
			return NetManager.PacketTypeStorage<T>.Module;
		}

		// Token: 0x06000E80 RID: 3712 RVA: 0x0000DFCA File Offset: 0x0000C1CA
		public ushort GetId<T>() where T : NetModule
		{
			return NetManager.PacketTypeStorage<T>.Id;
		}

		// Token: 0x06000E81 RID: 3713 RVA: 0x003FFD74 File Offset: 0x003FDF74
		public void Read(BinaryReader reader, int userId)
		{
			ushort key = reader.ReadUInt16();
			if (this._modules.ContainsKey(key))
			{
				this._modules[key].Deserialize(reader, userId);
			}
		}

		// Token: 0x06000E82 RID: 3714 RVA: 0x003FFDAC File Offset: 0x003FDFAC
		public void Broadcast(NetPacket packet, int ignoreClient = -1)
		{
			for (int i = 0; i < 256; i++)
			{
				if (i != ignoreClient && Netplay.Clients[i].IsConnected())
				{
					this.SendData(Netplay.Clients[i].Socket, packet);
				}
			}
		}

		// Token: 0x06000E83 RID: 3715 RVA: 0x0000DFD1 File Offset: 0x0000C1D1
		public void SendToServer(NetPacket packet)
		{
			this.SendData(Netplay.Connection.Socket, packet);
		}

		// Token: 0x06000E84 RID: 3716 RVA: 0x0000DFE4 File Offset: 0x0000C1E4
		public void SendToClient(NetPacket packet, int playerId)
		{
			this.SendData(Netplay.Clients[playerId].Socket, packet);
		}

		// Token: 0x06000E85 RID: 3717 RVA: 0x003FFDF0 File Offset: 0x003FDFF0
		private void SendData(ISocket socket, NetPacket packet)
		{
			if (Main.netMode == 0)
			{
				return;
			}
			packet.ShrinkToFit();
			try
			{
				socket.AsyncSend(packet.Buffer.Data, 0, packet.Length, new SocketSendCallback(NetManager.SendCallback), packet);
			}
			catch
			{
				Console.WriteLine(Language.GetTextValue("Error.ExceptionNormal", Language.GetTextValue("Error.DataSentAfterConnectionLost")));
			}
		}

		// Token: 0x06000E86 RID: 3718 RVA: 0x003FFE68 File Offset: 0x003FE068
		public static void SendCallback(object state)
		{
			((NetPacket)state).Recycle();
		}

		// Token: 0x04001243 RID: 4675
		public static readonly NetManager Instance = new NetManager();

		// Token: 0x04001244 RID: 4676
		private Dictionary<ushort, NetModule> _modules = new Dictionary<ushort, NetModule>();

		// Token: 0x04001245 RID: 4677
		private ushort _moduleCount;

		// Token: 0x0200014D RID: 333
		private class PacketTypeStorage<T> where T : NetModule
		{
			// Token: 0x04001246 RID: 4678
			public static ushort Id;

			// Token: 0x04001247 RID: 4679
			public static T Module;
		}
	}
}
